<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model {
    // 定义自动验证
    protected $_validate    =   array(
        array('username','require','用户名不能为空'),
        array('password','require','密码不能为空'),
        array('confirm_password','require','确认密码不能为空'),
    );

    /**
     *@param username:登录名  
     *@param password 登录密码   
     */
    public function checkLogin($loginInfo){
        $password = md5(base64_encode(md5($loginInfo['password'])).'576hbgh6');
        $where=array($loginInfo['username'],$password);
        return $this->where("username='%s' and password='%s'",$where)->find();
    }

    /**
	 * ps ：密码加密
	 * Time：2016/10/11 15:33:42
	 * @author liuxin
	 * @param 参数类型
	 * @return 返回值类型
	*/
	private function encrypt($password){
		return md5(base64_encode(md5($password)).'576hbgh6');
	}

    /**
     * 用户名是否已经存在
     * 
     */
    public function isExist($username){
        return  $this->where("username = '%s'",array($username))->find();
    }

    /**
     * 注册新用户
     * 
     */
    public function register($username,$password){
        $password = md5(base64_encode(md5($password)).'576hbgh6');
        return $this->add(array('username'=>$username ,'password'=>$password , 'reg_time'=>time()));
    }

    public function checkPassword($user_id,$password){
        $password = md5(base64_encode(md5($password)).'576hbgh6');
        $where=array(['user_id'=>$user_id]);
        $user = $this->where($where)->field('password')->find();
        if($password == $user['password']){
            return true;
        }
        else{
            return false;
        }
    }

    public function getUserName($user_id){
        $data = $this->where(['user_id'=>$user_id])->field('username,nickname')->find();
        return $data['nickname']?$data['nickname']:$data['username'];
    }
 }